<template>
  <div class="app-container">
    <div class="container">
      <div class="container-box container-left">
        <div class="text-primary" style="padding: 10px;font-weight: bold">常用联系人</div>
        <div class="people-list">
          <div v-for="(item,index) in peopleHub" :key="index" style="flex-direction: column">
            <div class="chat-item" @click="toChat(item.type,item.name)">
              <div class="chat-item-left">
                <el-avatar :size="70" :src="item.lastMessage != null ? item.lastMessage.sender.avatar:'http://localhost:8080/1.jpg'" />
              </div>
              <div class="chat-item-right text-info">
                <el-badge class="chat-item-user-info" :hidden="item.unread <= 0" :value="item.unread">
                  <div v-if="item.type === 'p2o'" class="chat-item-user-name text-primary" style="font-size: 18px">
                    {{ item.name }}
                  </div>
                  <div v-else-if="item.users != null && item.users.length > 1" class="chat-item-user-name text-primary" style="font-size: 18px">
                    {{ item.users['0'].id === $store.getters.id ? item.users['1'].name : item.users['0'].name }}
                  </div>
                  <div v-else-if="item.users != null" class="chat-item-user-name text-primary" style="font-size: 18px">
                    {{ item.users['0'].name }}
                  </div>
                  <div class="chat-item-user-org text-success" style="font-size: 15px">
                    {{ item.type === 'p2p' ? '个人通知': '社团通知' }}
                  </div>
                </el-badge>
              </div>
            </div>
            <div v-if="index < peopleHub.length - 1 " class="chat-item-divider" />
          </div>
        </div>
      </div>
      <div class="container-right">
        <div v-for="(type,index1) in typeMap" :key="index1" class="type-item">
          <div class="container-box">
            <!--            <el-badge :hidden="chartList[type.key].unread <= 0" :value="chartList[type.key].unread">-->
            <div class="type-title text-primary">{{ type.title }}</div>
            <!--            </el-badge>-->
            <div class="chat-list">
              <el-badge v-for="(item,index2) in chartList[type.key]" :key="index2" :hidden="item.unread <= 0" :value="item.unread" class="item">
                <div class="chat-item" @click="toChat(item.type,item.name)">
                  <div class="chat-item-left">
                    <el-avatar :size="70" :src="item.lastMessage.sender.avatar" />
                  </div>
                  <div class="chat-item-right text-info">
                    <div class="chat-item-user-info">
                      <div v-if="item.type === 'p2o'" class="chat-item-user-name text-primary" style="font-size: 18px">
                        {{ item.name }}
                      </div>
                      <div v-else-if="type.key === 'p2p' && item.users != null && item.users.length > 1" class="chat-item-user-name text-primary" style="font-size: 18px">
                        {{ item.users['0'].id === $store.getters.id ? item.users['1'].name : item.users['0'].name }}
                      </div>
                      <div v-else-if="item.users != null" class="chat-item-user-name text-primary" style="font-size: 18px">
                        {{ item.users['0'].name }}
                      </div>
                      <!--                      <div class="chat-item-user-org text-success" style="font-size: 15px">-->
                      <!--                        {{ item.org }}-->
                      <!--                      </div>-->
                    </div>

                    {{ item.lastMessage.sender.name + ':' +item.lastMessage.content }}
                  </div>
                </div>
              </el-badge>
            </div>
            <div v-if="chartList[type.key].length> 3" style="width: 100%;display: flex;flex-direction: column;align-items: center;justify-content: center">
              <div style="font-size: 20px;font-weight: bolder;color: #7d7d7f">· · ·</div>
              <el-button plain round>查看更多</el-button>
            </div>
          </div>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
import { getP2PList, getP2OList, getAllGroupByUid, getHistory } from '@/api/inform'
export default {
  name: 'InformIndex',
  filters: {},
  data() {
    return {
      id: this.$store.getters.id,
      maxNum: 3,
      chartList: {
        p2p: [],
        p2o: []
      },
      peopleHub: [],
      typeMap: [
        { title: '个人通知', key: 'p2p' },
        { title: '社团通知', key: 'p2o' }
        // { title: '广播通知', key: 'broadcast' }
      ]
    }
  },
  computed: {
    listenWebsocket() {
      return this.$store.state.websocket.message
    }
  },
  watch: {
    listenWebsocket: function(newstr) {
      const form = JSON.parse(newstr)
      if (form.message === 'new dialog') {
        const msg = Object.assign({}, JSON.parse(form.items))
        this.getList()
        if (msg.sender.id !== this.id) {
          this.$notify({
            title: msg.sender.name,
            message: (msg.content.length > 25 ? msg.content.substring(0, 25) + '...' : msg.content),
            type: 'success',
            duration: 4000
          })
        }
      }
    }
  },
  mounted() {
    this.getList()
  },
  methods: {
    toChat(type, gName) {
      getHistory({ page: 0, limit: 5, groupName: gName }).then(
        () => {
          this.getList()
          this.$router.push({ path: '/inform/' + type + '/' + gName + '/' + new Date().getTime() })
        }
      )
    },
    getList() {
      getP2PList().then(
        res => {
          this.chartList.p2p = []
          const p2pList = Object.assign([], res.data.items)
          for (let i = 0; i < this.maxNum && i < p2pList.length; i++) {
            p2pList[i].lastTime = new Date(p2pList[i].lastTime).getTime()
            this.chartList.p2p.push(p2pList[i])
          }
          console.log(res)
        }
      )
      getP2OList().then(
        res => {
          this.chartList.p2o = []
          const p2oList = Object.assign([], res.data.items)
          for (let i = 0; i < this.maxNum && i < p2oList.length; i++) {
            p2oList[i].lastTime = new Date(p2oList[i].lastTime).getTime()
            this.chartList.p2o.push(p2oList[i])
          }
        }
      )
      getAllGroupByUid(this.$store.getters.id).then(
        res => {
          this.peopleHub = []
          const all = Object.assign([], res.data.items)
          for (let i = 0; i < all.length; i++) {
            all[i].lastTime = new Date(all[i].lastTime).getTime()
            this.peopleHub.push(all[i])
          }
        }
      )
    }
  }
}
</script>
<style lang="scss" scoped>
  .container{
    width: auto;
    padding: 0;
    display: flex;
    flex-direction: row;
    align-items: flex-start;
    justify-content: center;
  }
  .type-title{
    font-weight: bolder;
    font-size: 25px;
    margin-bottom: 20px;
  }
  .chat-list{
    .item{
      width: 100%;
      margin: 20px 10px 0 0;
    }
    .chat-item{
      border: 1px solid #919499;
      padding: 10px;
      height: 95px;
      border-radius: 10px;
      display: flex;
      flex-direction: row;
      box-shadow: 0 2px 4px rgba(0, 0, 0, .12), 0 0 6px rgba(0, 0, 0, .04);
      -webkit-user-select:none;
      -moz-user-select:none;
      -o-user-select:none;
      user-select:none;
      .chat-item-left{
        display: flex;
        flex-direction: row;
        align-items: center;
        width: 90px;
      }
      .chat-item-right{
        width: calc(100% - 300px);
        overflow: hidden;
        text-overflow:ellipsis;
      }
      &:hover{
        background-color: #e8ebf1;
        cursor:pointer;
      }
      &:active{
        background-color: #e2e5eb;
      }
    }
  }
  .type-item{
    width: calc(100% - 0);
    margin: 30px 0;
    /*height: 520px;*/
    .container-box{
      /*width: calc(100vw - 400px);*/
      margin: 0;
      padding: 30px;
      border-radius: 5px;
      box-shadow: 0 2px 12px 0 rgba(0, 0, 0, 0.1);
    }
  }
  .container-left{
    position: absolute;
    left: 0px;
    top:10px;
    width: 250px;
    height: 85vh;
    padding: 10px 0;
    overflow: hidden;
    .people-list {
      position: absolute;
      height: 80vh;
      width: 270px;
      padding-right: 10px;
      /*padding-bottom: 10px;*/
      overflow-x: hidden;
      overflow-y: auto;
      display: flex;
      flex-direction: column;
      .item {
        margin-top: 10px;
        width: calc(100% - 20px);
      }
      .chat-item {
        margin: 10px;
        padding:5px 10px;
        height: 85px;
        display: flex;
        flex-direction: row;
        -webkit-user-select: none;
        -moz-user-select: none;
        -o-user-select: none;
        user-select: none;

        .chat-item-left {
          display: flex;
          flex-direction: row;
          align-items: flex-start;
          width: 90px;
        }

        .chat-item-right {
          width: 100%;
          padding: 10px;
          display: flex;
          flex-direction: column;
          text-overflow: ellipsis;
        }

        &:hover {
          background-color: #e8ebf1;
          cursor: pointer;
        }

        &:active {
          background-color: #e2e5eb;
        }
      }
    }
  }
  .container-right{
    position: absolute;
    height: calc(100vh - 100px);
    left: 300px;
    width:calc(100vw - 600px);
    padding-right:40px;
    overflow:scroll;
  }
  .chat-item-divider{
    height: 0;
    width: calc(100% - 20px);
    margin: 0 5px;
    border-bottom: 1px solid #b8b8bb;
  }
</style>
